package org.moflon.boardgameplatform.view;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.moflon.boardgameplatform.controller.Controller;

public class View extends ApplicationWindow {

	private Controller controller;
	private int height;
	private int width;
	
	private Field[][] board;

	/**
	 * Create the application window.
	 */
	public View(Controller controller, int width, int height) {
		super(null);
		
		this.controller = controller;
		this.height = height;
		this.width = width;
		
		board = new Field[width][height];
	}

	/**
	 * Create contents of the application window.
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(width, true));
		Color black = new Color(Display.getCurrent(), 255,255,255);
		Color white = new Color(Display.getCurrent(), 170,170,170);
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				Field field = new Field(i, j, controller, container, SWT.NONE, ((i+j)%2==0)?black:white);
				board[i][j] = field;
				
			}
		}
		
		controller.initializeBoard();
		
		return container;
	}

	/**
	 * Configure the shell.
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Board Game Platform");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point((width+1)*(Field.size), (height+2)*(Field.size));
	}

	public Field[][] getBoard() {
		return board;
	}
}
